﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WinnerCMS.Common;
using WinnerCMS.Common.Security;
using WinnerCMS.Data;
using WinnerCMS.Model;

#endregion

namespace WinnerCMS.BLL
{
    /// <summary>
    /// GuestBookManage 的摘要说明。
    /// </summary>
    public class GuestBookManage : MarshalByRefObject
    {
        private static readonly GuestBookDal Dal = new GuestBookDal();

        /// <summary>
        /// 绑定分页数据
        /// </summary>
        /// <param name="pp"></param>
        /// <returns></returns>
        public IList<GuestBook> BindList(PagingParam pp)
        {
            return Dal.BindList(pp);
        }

        #region 发送留言

        /// <summary>
        /// 发送留言
        /// </summary>
        /// <param name="model"></param>
        /// <param name="passCode"></param>
        public Result Add(GuestBook model, string passCode)
        {
            Result res = new Result();
            CMSConfig config = CMSConfig.Instance();
            if (config.DefendPROC.CheckPost && PostData.PostFormRight() == false)
            {
                res.Errors = "非法数据来源。";
                SecurityCenterManage.AppErr("发送添加留言数据来源非法。");
                return res;
            }

            if (HttpContext.Current.Session["VNum"] == null)
            {
                res.Errors = "请刷新该页使验证码更新";
                return res;
            }

            if (passCode != HttpContext.Current.Session["VNum"].ToString())
            {
                res.Errors = "填写的验证码与所给的不符";
                return res;
            }

            if (model.Name.Trim().Length == 0)
            {
                res.Errors = "名字不能留空！";
                return res;
            }
            if (model.Name.Trim().Length > 30)
            {
                res.Errors = "名字不能长过30字！";
                return res;
            }
            if (model.Content.Trim().Length == 0)
            {
                res.Errors = "留言内容不能留空！";
                return res;
            }
            if (model.Content.Trim().Length > 1000)
            {
                res.Errors = "留言簿不是论坛，请不要超过1000个字！";
                return res;
            }

            if (WordsManage.IsContainChinese(model.Content) == false)
            {
                res.Errors = "内容不包含中文！";
                return res;
            }

            if (WordsManage.IsContainBanWords(model.Content))
            {
                res.Errors = "留言内容含有广告词汇！";
                return res;
            }
            if (WordsManage.IsDataTooMuch("WinnerCMS_GuestBook"))
            {
                res.Errors = "警告：系统判断你可能在发送垃圾广告，因此你的IP已经被加入黑名单，如果你认为这是误判，请联系系统管理员解封！";
                return res;
            }


            if (KeYiPost(out res) == false)
            {
                return res;
            }


            model.Content = Encoder.HtmlEncode(model.Content);
            model.Content = HTML.Text2Html(model.Content);
            model.Content = WordsManage.FilterBadWords(model.Content);

            model.EMail = Encoder.HtmlEncode(model.EMail);
            model.QQ = model.QQ.Length == 0 ? "不告诉你" : Encoder.HtmlEncode(model.QQ);
            model.Home = model.Home.Length == 0 ? "不告诉你" : Encoder.HtmlEncode(model.Home);
            model.Face = Encoder.HtmlEncode(model.Face);
            model.Humor = Encoder.HtmlEncode(model.Humor);
            model.Reply = string.Empty;
            model.CreateTime = DateTime.Now;
            model.UpdateTime = DateTime.Now;
            Dal.Add(model);

            HttpCookie SendGBCookie = new HttpCookie("SendGBCookie");
            SendGBCookie.HttpOnly = true;
            SendGBCookie.Values.Add("UpdateTime", DateTime.Now.ToString());
            HttpContext.Current.Response.AppendCookie(SendGBCookie);

            return res;
        }

        /// <summary>
        /// 是否可以发送留言
        /// </summary>
        /// <returns></returns>
        private bool KeYiPost(out Result res)
        {
            res = new Result();
            if (HttpContext.Current.Request.Browser.Cookies == false)
            {
                res.Errors = "Cookie被禁止，发送留言操作终止！";
                return false;
            }

            HttpCookie SendGBCookie = HttpContext.Current.Request.Cookies["SendGBCookie"];

            if (SendGBCookie != null)
            {
                string UpdateTime = SendGBCookie.Values["UpdateTime"];
                TimeSpan stime = Convert.ToDateTime(UpdateTime).TimeOfDay;
                TimeSpan etime = DateTime.Now.TimeOfDay;
                if (etime.Subtract(stime).TotalSeconds >= 10)
                {
                    res.Errors = "";
                    return true;
                }
                res.Errors = "提交留言时间要间隔10秒！";
                return false;
            }
            res.Errors = "";
            return true;
        }

        #endregion

        /// <summary>
        /// 删除留言
        /// </summary>
        /// <param name="ids"></param>
        [Logging("删除留言：{@p0}"), ExceptionCall]
        public Result Delete(string ids)
        {
            Result res = new Result();
            Dal.Delete(MyConvert.ConvertToArr(ids));

            return res;
        }

        /// <summary>
        /// 删除指定IP的留言
        /// </summary>
        /// <param name="ip"></param>
        [Logging("删除IP为{@p0}的留言"), ExceptionCall]
        public Result DeleteForIP(string ip)
        {
            Result res = new Result();
            Dal.DeleteForIP(ip);

            return res;
        }

        /// <summary>
        /// 加载回复界面
        /// </summary>
        /// <param name="dl"></param>
        /// <param name="tb"></param>
        /// <param name="id"></param>
        public void ReplyLoad(Repeater dl, TextBox tb, string id)
        {
            GuestBook model = Dal.ReplyLoad(int.Parse(id));

            dl.DataSource = new[] {model};
            dl.DataBind();
            tb.Text = model.Reply;
        }

        /// <summary>
        /// 加载回复界面
        /// </summary>
        /// <param name="dl"></param>
        /// <param name="tb"></param>
        /// <param name="id"></param>
        public void ReplyLoad(DataList dl, TextBox tb, string id)
        {
            GuestBook model = Dal.ReplyLoad(int.Parse(id));
            dl.DataSource = new[] {model};
            dl.DataBind();
            tb.Text = model.Reply;
        }

        /// <summary>
        /// 发送回复
        /// </summary>
        /// <param name="id"></param>
        /// <param name="reply"></param>
        [Logging("回复留言ID：{@p0}")]
        public Result PostReply(string id, string reply)
        {
            Result res = new Result();
            Dal.PostReply(int.Parse(id), reply);
            return res;
        }
    }
}